package com.apestech.api.condition.conlib;

import com.apestech.api.condition.ConditionTableQrySql;
import com.apestech.framework.db.callback.TableJsonCallBackHandler;
import com.apestech.framework.dictionary.modul.Table;
import com.apestech.framework.json.JSONArray;
import com.apestech.framework.json.JSONObject;
import com.apestech.framework.xml.condition.modul.Condition;
import com.apestech.framework.xml.plugin.modul.SpecialField;
import com.apestech.framework.xml.table.modul.Tables;

import java.util.ArrayList;

public class ConQuery extends ConPub {

    public ConQuery(Condition condition) throws Exception {
        super(condition);
    }

    public JSONArray queryTableByM(String mid, JSONObject input) throws Exception {
        return queryTableByM(mid, input, null);
    }

    public JSONArray queryTableByM(String mid, JSONObject input, TableJsonCallBackHandler tableJsonCallBackHandler) throws Exception {
        String tabName=super.getCondition().getTableKey().getTabName(mid);
        return queryTable(tabName, input, tableJsonCallBackHandler);
    }

    public JSONArray queryTable(String tabName, JSONObject input) throws Exception {
        return queryTable(tabName, input, null);
    }

    public JSONArray queryTable(String tabName, JSONObject input, TableJsonCallBackHandler tableJsonCallBackHandler) throws Exception {
        Tables tab=super.getCondition().getTablesById(tabName);
        return super.getSession().getForDDL().queryMasterTable(tab, input, tableJsonCallBackHandler);
    }

    public String getSql(String mid) throws Exception {
        String tabName=super.getCondition().getTableKey().getTabName(mid);
        Table ztab=super.getSession().getDictTable(tabName);
        Table itab=super.getSession().getDictTable(tabName+"_ITEM");
        ArrayList<String> zkey=ztab.getField().getKeylist();
        ArrayList<String> ikey=itab.getField().getKeylist();
        String select="";
        SpecialField.Field sp=super.getSession().getSpecialField();
        int len=zkey.size();
        for(int i=0; i<len; i++){
            String k=zkey.get(i);
            if(!sp.getTimestamp().equals(k) && !sp.getVersion().equals(k) && !sp.getScene().equals(k) && !sp.getBilltype().equals(k)){
                if(!"".equals(select)){
                    select=select+",";
                }
                select=select+"A."+k;
            }
        }
        len=ikey.size();
        for(int i=0; i<len; i++){
            String k=ikey.get(i);
            if(!zkey.contains(k)){
                if(!"".equals(select)){
                    select=select+",";
                }
                select=select+"B."+k;
            }
        }
        return "SELECT "+select+" FROM "+tabName+" A,"+tabName+"_ITEM B WHERE A."+sp.getConditionId()+"=B."+sp.getConditionId();
    }

    public String getzTabSql_Single(String mid, JSONObject postJSON) throws Exception {
        String tabName=super.getCondition().getTableKey().getTabName(mid);
        ConditionTableQrySql sql=new ConditionTableQrySql(tabName, super.getSession());
        sql.setPostJSON(postJSON);
        return sql.getzTabSql_Single();
    }

    public String getzTabSql(String mid, JSONObject postJSON) throws Exception {
        String tabName=super.getCondition().getTableKey().getTabName(mid);
        ConditionTableQrySql sql=new ConditionTableQrySql(tabName, super.getSession());
        sql.setPostJSON(postJSON);
        return sql.getzTabSql();
    }

    public String getmxTabSql(String mid, JSONObject postJSON) throws Exception {
        String tabName=super.getCondition().getTableKey().getTabName(mid);
        ConditionTableQrySql sql=new ConditionTableQrySql(tabName, super.getSession());
        sql.setPostJSON(postJSON);
        return sql.getmxTabSql();
    }

    public String getexecTabSql(String mid, JSONObject postJSON, JSONArray gridheader) throws Exception {
        String tabName=super.getCondition().getTableKey().getTabName(mid);
        ConditionTableQrySql sql=new ConditionTableQrySql(tabName, super.getSession());
        sql.setPostJSON(postJSON);
        sql.setGridheader(gridheader);
        String s= sql.getexecTabSql();
        gridheader=sql.getGridheader();
        return s;
    }

    public String getimpTabSql(String mid, JSONObject postJSON, JSONArray gridheader) throws Exception {
        String tabName=super.getCondition().getTableKey().getTabName(mid);
        ConditionTableQrySql sql=new ConditionTableQrySql(tabName, super.getSession());
        sql.setPostJSON(postJSON);
        sql.setGridheader(gridheader);
        String s= sql.getimpTabSql();
        gridheader=sql.getGridheader();
        return s;
    }

    public String getStopTabSql(String mid, JSONObject postJSON, JSONArray gridheader) throws Exception {
        String tabName=super.getCondition().getTableKey().getTabName(mid);
        ConditionTableQrySql sql=new ConditionTableQrySql(tabName, super.getSession());
        sql.setPostJSON(postJSON);
        sql.setGridheader(gridheader);
        String s= sql.getStoptabSql();
        gridheader=sql.getGridheader();
        return s;
    }

    public String getbillQrySql(String mid, JSONObject postJSON, JSONArray gridheader) throws Exception {
        String tabName=super.getCondition().getTableKey().getTabName(mid);
        ConditionTableQrySql sql=new ConditionTableQrySql(tabName, super.getSession());
        sql.setPostJSON(postJSON);
        sql.setGridheader(gridheader);
        sql.setFormJSON(super.getCondition().getFormJSON());
        String s=sql.getbillQrySql();
        gridheader=sql.getGridheader();
        return s;
    }

}
